home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.09 Sep 91 / MW_Source Folder / MeterWindow next >
Encoding:
Text File  |  1990-12-01  |  5.4 KB  |  189 lines  |  [TEXT/PJMM]

  1. {------------------------------------------------}
  2. {        MeterWindow Unit.                                }
  3. {    This unit contains all the functions and             }
  4. {    procedures needed to support the     initializing,        }
  5. {    displaying, updating, and destroying of the            }
  6. {    Meter Window.                                            }
  7.  
  8. unit MeterWindow;
  9.  
  10. interface
  11.  
  12.     procedure mWindowInit;
  13.  
  14.     procedure mWindowDraw;
  15.  
  16.     procedure mWindowTitle (mStr: Str255);
  17.  
  18.     procedure mWindowUpdate (curI, maxI: longint);
  19.  
  20.     procedure mWindowKill;
  21.  
  22. implementation
  23.  
  24. {    Constants to control the size and placement of the Meter Window title and meter box.}
  25.     const
  26.         mRx = 13;
  27.         mRy = 24;
  28.         mRw = 250;
  29.         mRh = 16;
  30.  
  31.         tRx = 13;
  32.         tRy = 1;
  33.         tRw = 253;
  34.         tRh = 16;
  35.  
  36.     var
  37.         mWPtr: WindowPtr;
  38.  
  39. {--------------------------------------------------------------------------------------------}
  40. {        mWindowInit procedure                                                                                    }
  41. {    This procedure initializes the meter window and draw it in the middle of the screen.                    }
  42.  
  43.     procedure mWindowInit;
  44.  
  45. {    The constants for the height and width of the Meter Window.}
  46.         const
  47.             mWwidth = 280;
  48.             mWheight = 60;
  49.  
  50.         var
  51.             savePort: GrafPtr;
  52.             iRect: Rect;
  53.             mwXOffset, mwYOffset: integer;
  54.  
  55.     begin
  56.         SetRect(iRect, 0, 0, mWwidth, mWheight);
  57. {    Create the Meter Window with a window definition ID equal to 1.}
  58.         mWPtr := NewWindow(nil, iRect, '', false, 1, Pointer(-1), false, longint(0));
  59.         iRect := screenBits.bounds;
  60. {    Figure out the x and y offsets in pixels to put the Meter Window in the center of this }
  61. {    screen.  After that move the window there and display the empty window.}
  62.         mwXOffset := integer(round(((iRect.right - iRect.left) - (mWptr^.portRect.right - mWptr^.portRect.left)) / 2));
  63.         mwYOffset := integer(round(((iRect.bottom - iRect.top) - (mWptr^.portRect.bottom - mWptr^.portRect.top)) / 2));
  64.         MoveWindow(mWPtr, mwXOffset, mwYOffset, false);
  65.         ShowWindow(mWPtr);
  66.     end;
  67.  
  68. {--------------------------------------------------------------------------------------------}
  69. {        mWindowDraw procedure                                                                                }
  70. {    This procedure draws the meter box with its graduations and the corresponding annotation.            }
  71.  
  72.     procedure mWindowDraw;
  73.  
  74. {    These constatns control the placement of the 5% and 10% graduations in the meter box based}
  75. {    on a meter box that is 250 pixels wide.}
  76.         const
  77.             fiveSize = 1;
  78.             fiveStep = 12;
  79.             tenSize = 3;
  80.             tenStep = 25;
  81.  
  82.         var
  83.             i: integer;
  84.             mRect: Rect;
  85.             savePort: GrafPtr;
  86.  
  87.     begin
  88.         GetPort(savePort);
  89.         SetPort(mWPtr);
  90. {    Erase the Meter Window in case something is already drawn there.}
  91.         EraseRect(mWPtr^.portRect);
  92. {    Set up the meter box in the mRect rectangle.}
  93.         SetRect(mRect, mRx, mRy, mRx + mRw, mRy + mRh);
  94.         TextFont(0);
  95.         TextSize(12);
  96.         PenSize(1, 1);
  97. {    Draw the frame around the meter box rectangle.}
  98.         InsetRect(mRect, -1, -1);
  99.         FrameRect(mRect);
  100.         InsetRect(mRect, 1, 1);
  101. {    Draw the first 5% graduation then let the following loop do the rest.}
  102.         MoveTo(mRect.left + fiveStep, mRect.top - 1);
  103.         Line(0, fiveSize);
  104.         MoveTo(mRect.left + fiveStep, mRect.bottom);
  105.         Line(0, -fiveSize);
  106.         for i := 1 to 9 do
  107.             begin
  108.                 MoveTo(mRect.left + (i * tenStep) - 1, mRect.top - 1);
  109.                 Line(0, tenSize);
  110.                 MoveTo(mRect.left + (i * tenStep) - 1, mRect.bottom);
  111.                 Line(0, -tenSize);
  112.                 MoveTo(mRect.left + (i * tenStep) + fiveStep, mRect.top - 1);
  113.                 Line(0, fiveSize);
  114.                 MoveTo(mRect.left + (i * tenStep) + fiveStep, mRect.bottom);
  115.                 Line(0, -fiveSize);
  116.             end;
  117. {    Now draw the appropriate meter box annotation.}
  118.         MoveTo(mRect.left + 65, mRect.bottom + mRh);
  119.         DrawString('Percent Complete');
  120.         MoveTo(mRect.left - 5, mRect.bottom + mRh);
  121.         DrawString('0%');
  122.         MoveTo(mRect.right - 20, mRect.bottom + mRh);
  123.         DrawString('100%');
  124.         SetPort(savePort);
  125.     end;
  126.  
  127. {--------------------------------------------------------------------------------------------}
  128. {        mWindowTitle procedure                                                                                    }
  129. {    This procedure draws the title of the meter window in the tRect rectangle.}
  130.  
  131.     procedure mWindowTitle;
  132.  
  133.         var
  134.             savePort: GrafPtr;
  135.             tRect: Rect;
  136.  
  137.     begin
  138.         GetPort(savePort);
  139.         SetPort(mWPtr);
  140. {    Set up the title rectangle in the tRect structure, erase the rectangle, then draw the}
  141. {    title string in the rectangle.}
  142.         SetRect(tRect, tRx, tRy, tRx + tRw, tRy + tRh);
  143.         EraseRect(tRect);
  144.         MoveTo(tRect.left, tRect.bottom);
  145.         DrawString(mStr);
  146.         SetPort(savePort);
  147.     end;
  148.  
  149. {--------------------------------------------------------------------------------------------}
  150. {        mWindowUpdate procedure                                                                                }
  151. {    This procedure fills in the meter box based on the curI and maxI parameters.  CurI is the}
  152. {    current step in the process with maxI steps.}
  153.  
  154.     procedure mWindowUpdate;
  155.  
  156.         var
  157.             pDone: integer;
  158.             mRect: Rect;
  159.             savePort: GrafPtr;
  160.  
  161.     begin
  162.         GetPort(savePort);
  163.         SetPort(mWPtr);
  164. {    Determine which percent of the process is done and load it in pDone.}
  165.         if maxI = 0 then
  166.             pDone := 0
  167.         else
  168.             pDone := integer(round(curI / maxI * 100));
  169. {    Just in case the percent done is greater than 100%.}
  170.         if pDone > 100 then
  171.             pDone := 100;
  172.         SetRect(mRect, mRx, mRy, mRx + mRw, mRy + mRh);
  173. {    Since the meter box is 250 pixels wide, each percent corresponds to 2.5 pixels.}
  174.         mRect.right := mRect.left + integer(round((pDone * 2.5)));
  175.         FillRect(mRect, black);
  176.         SetPort(savePort);
  177.     end;
  178.  
  179. {--------------------------------------------------------------------------------------------}
  180. {        mWindowKill procedure                                                                                    }
  181. {    This procedure disposes of the Meter Window data structure.}
  182.  
  183.     procedure mWindowKill;
  184.  
  185.     begin
  186.         DisposeWindow(mWPtr);
  187.     end;
  188.  
  189. end.